﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Function;
using BLL;
using Model;

namespace UI
{
    public partial class ModifyPassword : System.Web.UI.Page
    {
        
        MessageBox box = new MessageBox();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["type"] != null && Session["type"].ToString() == "getpwd")
                {
                    ST_Investor model = new ST_InvestorBLL().GetModel(Session["phone"].ToString().Trim());
                    this.lblphone.Text = Session["phone"].ToString().Substring(0, 3) + "*****" + Session["phone"].ToString().Substring(8, 3);
                    this.lblLogin.Text = model.Email;
                }
                else
                {
                    box.SuccessBox(this, "拒绝访问!", "Index.aspx");
                }
            }
        }

        protected void button_back_Click(object sender, EventArgs e)
        {
            Response.Redirect("Index.aspx");
        }

        protected void btnreset_Click(object sender, EventArgs e)
        {
            ST_InvestorBLL investor = new ST_InvestorBLL();
            if (string.IsNullOrEmpty(txtnewpwd.Text) || txtnewpwd.Text == "")
            {
                box.ErrorBox(this, "新密码不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(txtnewpwds.Text) || txtnewpwds.Text == "")
            {
                box.ErrorBox(this, "重复密码不能为空!");
                return;
            }
            if (txtnewpwd.Text != txtnewpwds.Text)
            {
                box.ErrorBox(this, "二次输入密码不一致!");
                return;
            }
            if (!investor.ExistsByMainTel(Session["phone"].ToString()))
            {
                box.ErrorBox(this, "不存在该用户!");
                return;
            }
            else
            {
                string pwd = this.txtnewpwd.Text.Trim();
                ST_Investor model = new ST_Investor ();
                model = investor.GetModel(Session["phone"].ToString().Trim());
                model.LoginPw = pwd.getPwd();
                model.LoginPWChanged = 2;
                if (investor.Update(model))
                {
                    box.SuccessBox(this, "重置密码成功!", "Index.aspx");
                }
                else
                {
                    box.ErrorBox(this, "重置密码失败!");
                    return;
                }
            }
        }
    }
}